package com.whfc.ms.api.energy.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.env.entity.EnergyDevice;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.energy.service.MsEnergyDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@RequiresPermissions("/energy/device")
@RestController
@RequestMapping("/ms/api/energyDevice")
public class EnergyDeviceController extends BaseController {

    @Autowired
    private MsEnergyDeviceService msEnergyDeviceService;

    @GetMapping(value = "/list", params = {"pageNum", "pageSize"})
    public Result deviceList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                             @RequestParam("pageNum") Integer pageNum,
                             @RequestParam("pageSize") Integer pageSize,
                             @RequestParam("deptId") Integer deptId,
                             @RequestParam(value = "name", required = false) String name,
                             @RequestParam(value = "type", required = false) Integer type) {
        logger.info("获取能耗设备列表分页：loginer:{},pageNum:{},pageSize:{},deptId:{}",
                user.getUsername(), pageNum, pageSize, deptId);
        PageData<EnergyDevice> pageData = msEnergyDeviceService.listPage(deptId, pageNum, pageSize,name,type);
        return ResultUtil.success(pageData);
    }

    @GetMapping(value = "/list")
    public Result deviceList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                             @RequestParam("deptId") Integer deptId,
                             @RequestParam(value = "name", required = false) String name,
                             @RequestParam(value = "type", required = false) Integer type
                             ) {
        logger.info("获取能耗设备列表：loginer:{},deptId:{}",
                user.getUsername(),  deptId);
        List<EnergyDevice> list = msEnergyDeviceService.list(deptId,name,type);
        return ResultUtil.success(list);
    }

    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @RequestBody EnergyDevice request) {
        logger.info("添加能耗设备：loginer:{},request:{}", user.getUsername(), request.toString());
        msEnergyDeviceService.add(request);
        return ResultUtil.success();
    }

    @PostMapping("/edit")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestBody EnergyDevice request) {
        logger.info("编辑能耗设备：loginer:{},request:{}", user.getUsername(), request.toString());
        msEnergyDeviceService.edit(request);
        return ResultUtil.success();
    }

    @PostMapping("/del/{id}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @PathVariable("id") Integer id) {
        logger.info("删除能耗设备：loginer:{},id:{}", user.getUsername(), id);
        msEnergyDeviceService.del(id);
        return ResultUtil.success();
    }

}
